package com.rightsidetech.http.controller.admin.trade;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.*;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.response.CommodityImportResp;
import com.rightsidetech.mall.entity.MallCommoditySpecs;
import com.rightsidetech.mall.service.MallCommodityService;
import com.rightsidetech.system.dtos.response.store.SysStoreOrderDetailResp;
import com.rightsidetech.system.entity.SysLevelRule;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.service.LevelRuleService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import com.rightsidetech.trade.dtos.request.*;
import com.rightsidetech.trade.dtos.response.*;
import com.rightsidetech.trade.entity.TdCommodityOrderDetails;
import com.rightsidetech.trade.entity.TdCommodityTradeOrder;
import com.rightsidetech.trade.entity.TdCommodityUserOrder;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.dtos.requst.IdReq;
import com.rightsidetech.user.dtos.response.UserImportResp;
import com.rightsidetech.user.entity.UsUserAddress;
import com.rightsidetech.user.entity.UsUserInfo;
import com.rightsidetech.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/admin/trade")
@ShenyuSpringMvcClient(path = "/admin/trade/**")
@Api(tags = "admin:用户订单相关")
public class TdCommodityController extends BaseController {


    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;
    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private MallCommodityService mallCommodityService;
    @Resource
    private LevelRuleService levelRuleService;
    @Value("${restapi.amap.addressUrl}")
    private String addressUrl;
    @Value("${restapi.amap.key}")
    private String amapKey;
    @Value("${storeCodeUrl}")
    private String codeUrl;

    @Log
    @Token
    @ApiOperation("用户订单列表")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    public ResponseData<PageInfo<TdCommodityUserOrderResp>> page(TdCommodityOrderListRequest req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if (loginInfo.getSysCompanyAreaId() != 0) {
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return tdCommodityUserOrderService.page(req);
    }

    @Log
    @Token
    @ApiOperation("商城账单")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/billPage")
    public ResponseData<PageInfo<TdCommodityTradeOrderResp>>listTradeOrder(TdCommodityTradeOrderReq  req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null){
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(),validate.getMessage());
        }
       return tdCommodityUserOrderService.listTradeOrder(req);
    }

    @Log
    @Token
    @ApiOperation("用户订单详细")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detail")
    public ResponseData<TdCommodityDetailResp> getByOrderId(@ApiParam(required = true, name = "id", value = "id") @RequestParam("id") Long id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "id不能为空");
        }
        return tdCommodityUserOrderService.getByOrderId(id);
    }

    @Log
    @Token
    @ApiOperation("订单的商品物流单号信息")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/trackDetail")
    public ResponseData<TdCommodityUserOrderZpResp> getOrderDetailTrackNo(TdCommodityOrderNoRequest req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return tdCommodityUserOrderService.getOrderDetailTrackNo(req.getOrderNo());
    }

    @Log
    @Token
    @ApiOperation("修改用户订单 门店/订单状态 信息")
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/updateOrderMsg")
    public ResponseData updateOrderMsg(@RequestBody TdCommodityUpdateOrderMsgReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreOrderDetailResp sysStoreOrderDetailResp = sysStoreInfoService.findOrderDetailByStoreId(req.getStoreId());
        if (StringUtils.isEmpty(sysStoreOrderDetailResp)) {
            return ResponseData.fail(CodeIdEnum.NonStoreInfo);
        }

        return tdCommodityUserOrderService.updateOrderMsg(req, sysStoreOrderDetailResp.getStoreName(), sysStoreOrderDetailResp.getAgentId(), sysStoreOrderDetailResp.getAgentName(), sysStoreOrderDetailResp.getAreaId(), sysStoreOrderDetailResp.getAreaName());
    }


    @Log
    @Token
    @ApiOperation("换购商品订单列表")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/gift_page")
    public ResponseData<PageInfo<CommodityUserGiftResp>> page(TdCommodityGiftOrderReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if (loginInfo.getSysCompanyAreaId() != 0) {
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return tdCommodityUserOrderService.giftPage(req);
    }

    @Log
    @Token
    @ApiOperation("修改订单物流单号")
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/updateTrackNo")
    public ResponseData updateTrackNoById(@RequestBody TdCommodityUpdateTrackNoReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        return tdCommodityUserOrderService.updateTrackNoById(req,loginInfo.getId(),loginInfo.getLoginName());
    }

    @Log
    @Token
    @ApiOperation("订单售后列表")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/afterServicePage")
    public ResponseData<PageInfo<TdCommodityAfterPageResp>> afterServicePage(TdCommodityAfterServiceReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if (loginInfo.getSysCompanyAreaId() != 0) {
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return tdCommodityUserOrderService.afterServicePage(req);
    }

    @Log
    @Token
    @ApiOperation("订单售后退款")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/afterServiceMoney")
    public ResponseData<PageInfo<TdCommodityAfterServiceMoneyResp>> afterServiceMoneyPage(TdCommodityAfterServiceMoneyReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if (loginInfo.getSysCompanyAreaId() != 0) {
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }
        return tdCommodityUserOrderService.afterServiceMoneyPage(req);
    }

    @Log
    @Token
    @ApiOperation("订单售后详细")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/afterServiceDetail")
    public ResponseData<TdCommodityAfterServiceDetailResp> findAfterServiceDetail(IdReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return tdCommodityUserOrderService.findAfterServiceDetailById(req.getId());
    }


  /*  @Log
    @Token
    @ApiOperation("商城账单")
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/billPage")
    public ResponseData<PageInfo<TdCommodityBillResp>> findOrderBillPage(TdCommodityBillReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if (loginInfo.getSysCompanyAreaId() != 0) {
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return tdCommodityUserOrderService.findCommodityBillPage(req);
    }*/

    @Log
    @Token
    @ApiOperation("订单导入")
    //@Auth(bizType = Auth.OPERATOR)
    @PostMapping("/import")
    //@ApiIgnore
    public ResponseData ImportExcel(@RequestParam MultipartFile file) {
        try {
            //判断是否是excel
            String fileName = file.getOriginalFilename();
            if (StringUtils.isEmpty(fileName) || !fileName.contains(".")) {
                return ResponseData.fail("格式不正确");
            }
            String lastName = fileName.substring(fileName.lastIndexOf("."));
            if (!".xls".equals(lastName) && !".xlsx".equals(lastName)) {
                return ResponseData.fail("格式不正确");
            }

            List<List<Object>> excelData = ImportExcelUtils.importExcelMultipartFile(file, 2, 0, TdCommodityImportReq.class);
            if (excelData.isEmpty()) {
                return new ResponseData(CodeIdEnum.EMPTY_DATA.getValue(), "数据为空，无法导入");
            }
            //将Excel中数据，转为实体类
            List<List<TdCommodityImportReq>> alarmList = new ArrayList<>();
            for (List<?> list : excelData) {
                List<TdCommodityImportReq> list1 = JSONArray.parseArray(JSONArray.toJSONString(list), TdCommodityImportReq.class);
                alarmList.add(list1);
            }
            if (alarmList.get(0).isEmpty()) {
                return new ResponseData(CodeIdEnum.EMPTY_DATA.getValue(), "数据为空，无法导入");
            }
            //校验订单编号是否已存在或格式错误
            ResponseData res = checkOrderList(alarmList.get(0));
            if (res.getCodeId() != 1) {
                return res;
            }

            //return ResponseData.success(alarmList.get(0));
            List<TdCommodityUserOrder> orderList = new ArrayList<>();
            List<TdCommodityOrderDetails> orderDetailsList = new ArrayList<>();

            //校验导入的订单,
            //1.找不到联系人(注册帐号-手机号, 添加收货址)
            List<UserImportResp> newUserList = new ArrayList<>();
            //2.所属门店存在
            //3.商品名称, 分类, 品牌
            List<TdCommodityImportReq> list = alarmList.get(0);
            int count = 1;
            for (TdCommodityImportReq importReq : list) {

                //判断是否存在重复的商品的订单
                boolean orderFlag = true;
                TdCommodityUserOrder order = null;
                if (orderList.size() > 0) {
                    for (TdCommodityUserOrder tdCommodityUserOrder : orderList) {
                        if (tdCommodityUserOrder.getOtherPaltformsOrderNo().equals(importReq.getOtherPaltformsOrderNo())) {
                            orderFlag = false;
                            order = tdCommodityUserOrder;
                        }
                    }
                }

                if (orderFlag) {
                    order = new TdCommodityUserOrder();
                    TdCommodityUserOrder td3 = tdCommodityUserOrderService.selectByOtherPaltformsOrderNo(importReq.getOtherPaltformsOrderNo());
                    if (StringUtils.isEmpty(td3)) {
                        String orderNo = DateUtil.getDate(new Date(), "yyyyMMddHHmmss") + (int) (Math.random() * 900 + 100);
                        order.setOrderNo(orderNo);
                    } else {
                        order.setOrderNo(td3.getOrderNo());
                    }
                    order.setTradeChannel((byte) 2);
                    order.setUsName(importReq.getUsName());
                    order.setUsPhoneNo(importReq.getUsPhoneNo());
                    order.setUsAddress(importReq.getUsAddress());
                    //order.setLongitude();
                    //order.setLatitude();
                    order.setCreatedAt(DateUtil.parseDateStr(importReq.getCreatedAt()));
                    order.setPayTime(DateUtil.parseDateStr(importReq.getPayTime()));
                    //待支付|已接单|已完成
                    int orderStatus = 1;
                    order.setOrderStatus((byte) orderStatus);
                    order.setTotalAmount(new BigDecimal(importReq.getTotalAmount()).multiply(new BigDecimal("100")));
                    order.setDiscountAmount(new BigDecimal(importReq.getDiscountAmount()).multiply(new BigDecimal("100")));
                    order.setActualAmount(new BigDecimal(importReq.getActualAmount()).multiply(new BigDecimal("100")));
                    int orderType = 0;
                    order.setOrderType((byte) orderType);
                    order.setOtherPaltformsOrderNo(importReq.getOtherPaltformsOrderNo());

                    Byte source = 0;
                    switch (importReq.getSource()) {
                        case "淘宝":
                            source = 1;
                            break;
                        case "京东":
                            source = 2;
                            break;
                        case "抖音":
                            source = 3;
                            break;
                        case "拼多多":
                            source = 4;
                            break;
                        case "天猫":
                            source = 5;
                            break;
                        case "苏宁":
                            source = 6;
                            break;
                        default:
                            source = 7;
                            break;
                    }
                    order.setSource(source);

                    //查询收货地址经纬度
                    UsUserAddress usUserAddress = findByAddress(importReq.getUsAddress());
                    //根据经纬度查找最近门店
                    SysStoreInfo sysStoreInfo3 = sysStoreInfoService.findNearStoreInfo(usUserAddress.getLongitude(), usUserAddress.getLatitude());
                    if (StringUtils.isEmpty(sysStoreInfo3)) {
                        return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据, 找不到符合收货地址的门店: " + importReq.getUsAddress());
                    }

                    order.setStoreName(sysStoreInfo3.getStoreName());

                    //根据手机号查询
                    UsUserInfo usUserInfo = userInfoService.findByPhoneNo(importReq.getUsPhoneNo());
                    if (StringUtils.isEmpty(usUserInfo)) {
                        UserImportResp userImportResp = new UserImportResp();
                        userImportResp.setPhoneNo(importReq.getUsPhoneNo());
                        userImportResp.setRealName(importReq.getUsName());
                        userImportResp.setAddress(importReq.getUsAddress());
                        userImportResp.setLatitude(usUserAddress.getLatitude());
                        userImportResp.setLongitude(usUserAddress.getLongitude());
                        userImportResp.setProvince(usUserAddress.getProvince());
                        userImportResp.setCity(usUserAddress.getCity());
                        userImportResp.setArea(usUserAddress.getArea());
                        newUserList.add(userImportResp);
                    } else {
                        order.setUserInfoId(usUserInfo.getId());
                    }
                    //判断门店, 代理商, 大区
                    //根据经纬度找到订单门店
                    SysStoreOrderDetailResp sysStoreInfo2 = sysStoreInfoService.findOrderDetailByStoreId(sysStoreInfo3.getId());
                    if (StringUtils.isEmpty(sysStoreInfo2)) {
                        return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据, 找不到符合收货地址的门店: " + importReq.getUsAddress());
                    }
                    order.setStoreId(sysStoreInfo2.getStoreId());
                    order.setStoreName(sysStoreInfo2.getStoreName());
                    order.setAgentId(sysStoreInfo2.getAgentId());
                    order.setAgentName(sysStoreInfo2.getAgentName());
                    order.setAreaId(sysStoreInfo2.getAreaId());
                    order.setAreaName(sysStoreInfo2.getAreaName());
                    order.setLongitude(usUserAddress.getLongitude());
                    order.setLatitude(usUserAddress.getLatitude());

                    orderList.add(order);
                }

                //查询商品名称, 分类信息, 品牌信息
                CommodityImportResp commodityImportResp = mallCommodityService.selectCommodityByCommodityName(importReq.getCommodityName());
                if (StringUtils.isEmpty(commodityImportResp)) {
                    return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据, 找不到符合的商品名称: " + importReq.getCommodityName());
                }
                TdCommodityOrderDetails details = new TdCommodityOrderDetails();
                //查询商品规格编码,
                if (!StringUtils.isEmpty(importReq.getCommoditySpecs())) {
                    //查询规格编码
                    MallCommoditySpecs mallCommoditySpecs = mallCommodityService.selectSpecsByCommodityAndSpece(commodityImportResp.getCommodityId(), importReq.getCommoditySpecs());
                    if (StringUtils.isEmpty(mallCommoditySpecs)) {
                        return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据, 找不到符合的商品规格编码: " + importReq.getCommoditySpecs());
                    }
                    details.setCommodityBarCode(mallCommoditySpecs.getBarCode());
                }

                details.setCommodityId(commodityImportResp.getCommodityId());
                details.setOrderNo(order.getOrderNo());
                details.setCommodityName(importReq.getCommodityName());
                details.setCommodityPrice(new BigDecimal(importReq.getCommodityPrice()).multiply(new BigDecimal("100")));
                details.setCommodityNum(importReq.getCommodityNum());
                details.setCommoditySpecs(importReq.getCommoditySpecs());
                details.setParentId(0L);
                details.setPrice(new BigDecimal(importReq.getCommodityPrice()).multiply(new BigDecimal("100")));
                details.setUnderlinedPrice(new BigDecimal(importReq.getCommodityPrice()).multiply(new BigDecimal("100")));
                details.setTypeName(commodityImportResp.getTypeName());
                details.setTypeId(commodityImportResp.getTypeId());
                details.setBrandName(commodityImportResp.getBrandName());
                details.setBrandId(commodityImportResp.getBrandId());
                details.setCommodityTag((byte) 0);
                int buyType = importReq.getBuyType().equals("直接买新") ? 0 : 1;
                details.setBuyType((byte) buyType);
                details.setOrderStatus(order.getOrderStatus());
                details.setCreatedAt(DateUtil.parseDateStr(importReq.getCreatedAt()));

                orderDetailsList.add(details);

            }

            //注册用户
            if (newUserList.size() > 0) {

                SysLevelRule levelRule = levelRuleService.findLevelRuleByOrderNum(0L);
                String levelName = null;
                Byte level = 0;
                if (!ObjectUtils.isEmpty(levelRule)) {
                    levelName = levelRule.getLevelName();
                    level = levelRule.getLevel();
                }

                for (UserImportResp usUserInfo : newUserList) {
                    UsUserAddress usUserAddress = new UsUserAddress();
                    usUserAddress.setAddress(usUserInfo.getAddress());
                    usUserAddress.setLatitude(usUserInfo.getLatitude());
                    usUserAddress.setLongitude(usUserInfo.getLongitude());
                    usUserAddress.setProvince(usUserInfo.getProvince());
                    usUserAddress.setCity(usUserInfo.getCity());
                    usUserAddress.setArea(usUserInfo.getArea());
                    userInfoService.registerImportOrder(usUserInfo.getPhoneNo(), getMaxMemberNo(), codeUrl + getMaxMemberNo(), level, levelName, usUserInfo.getRealName(), usUserAddress);

                }
                //设置用户id
                for (TdCommodityUserOrder td2 : orderList) {
                    for (UserImportResp usUserInfo : newUserList) {
                        if (td2.getUsPhoneNo().equals(usUserInfo.getPhoneNo())) {
                            td2.setUserInfoId(usUserInfo.getId());
                            /*td2.setLongitude(usUserInfo.getLongitude());
                            td2.setLatitude(usUserInfo.getLatitude());*/
                        }
                    }
                }
            }

            //插入订单数据
            return tdCommodityUserOrderService.insetTdCommodityList(orderList, orderDetailsList);
        } catch (Exception e) {
            return ResponseData.fail("订单导入失败" + e);
        }
    }

    private synchronized String getMaxMemberNo() {
        long memberNo = 10001L;
        String maxMemberNo = userInfoService.getMaxMemberNo();
        if (!StringUtils.isEmpty(maxMemberNo)) {
            memberNo = Long.parseLong(maxMemberNo.replace("YS", "")) + 1;
        }
        return "YS" + memberNo;
    }


    private UsUserAddress findByAddress(String address) throws Exception {
        UsUserAddress usUserAddress = new UsUserAddress();
        //查询省市区
        Map map = new HashMap<>();
        map.put("key", amapKey);
        map.put("address", address);
        JSONObject jsonObject = JSON.parseObject(HttpClient.sendGet(addressUrl, map));
        if (!StringUtil.isNullorEmpty(jsonObject) && jsonObject.getString("status").equals("1") && !StringUtil.isNullorEmpty(jsonObject.get("geocodes"))) {
            JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
            JSONObject jsonObject2 = jsonArray.getJSONObject(0);

            if (!StringUtil.isNullorEmpty(jsonObject2.getString("location"))) {
                String[] lonAndLat = jsonObject2.getString("location").split(",");
                usUserAddress.setLongitude(new BigDecimal(lonAndLat[0]));
                usUserAddress.setLatitude(new BigDecimal(lonAndLat[1]));
                usUserAddress.setProvince(jsonObject2.getString("province"));
                usUserAddress.setCity(jsonObject2.getString("city"));
                usUserAddress.setArea(jsonObject2.getString("district"));
            } else {
                return null;
            }
        } else {
            return null;
        }
        return usUserAddress;
    }

    public ResponseData checkOrderList(List<TdCommodityImportReq> list) {

        int count = 1;

        for (TdCommodityImportReq req : list) {
            ConstraintViolation validate = BeanValidator.validate(req);
            if (validate != null) {
                return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据异常: " + validate.getMessage());
            }

            //校验订单
            TdCommodityUserOrder tdCommodityUserOrder = tdCommodityUserOrderService.selectByOtherPaltformsOrderNo(req.getOtherPaltformsOrderNo());
            if (!StringUtils.isEmpty(tdCommodityUserOrder)) {
                return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), "第" + count + "行数据异常，订单编号: " + req.getOtherPaltformsOrderNo() + " 已存在");
            }
            count++;
        }


        return ResponseData.success();
    }


/*    @Log
    @ApiOperation("测试最近地址")
    @PostMapping("/getNearStore")
    public ResponseData getNearStoreInfo(@RequestBody UsUserAddress usUserAddress){
        SysStoreInfo sysStoreInfo = sysStoreInfoService.findNearStoreInfo(usUserAddress.getLongitude(),usUserAddress.getLatitude());
        return ResponseData.success(sysStoreInfo);
    }*/

}
